package com.zjd.association.controller;

import com.zjd.association.utils.UUIDUtils;
import com.zjd.association.common.R;
import com.zjd.association.entity.Activities;
import com.zjd.association.entity.Users;
import com.zjd.association.handle.CacheHandle;
import com.zjd.association.service.ActivitiesService;
import com.zjd.association.service.UsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 活动信息 控制层
 *
 * @author:zjd
 * @date: 2024-5-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/activities")
public class ActivitiesController {

    private final CacheHandle cacheHandle;

    private final UsersService usersService;

    private final ActivitiesService activitiesService;

    /**
     * 获取路径
     *
     * @return 路径
     */
    @RequestMapping("")
    public String index() {
        return "pages/Activities";
    }

    /**
     * 通过id 查询活动信息
     *
     * @param id 活动id
     * @return 活动信息
     */
    @GetMapping("/info")
    public R getInfo(String id) {
        Activities activities = activitiesService.getOne(id);
        return R.ok(activities);
    }

    /**
     * 查询活动信息 分页
     *
     * @param pageIndex  分页页数
     * @param pageSize   分页大学
     * @param token      用户token
     * @param teamName   活动名称
     * @param activeName 活动名称
     * @return 活动信息
     */
    @GetMapping("/page")
    public R getPageInfos(Long pageIndex, Long pageSize, String token, String teamName, String activeName) {
        //获取用户信息
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
//dra
        if (user.getType() == 0) {
            //管理员查询全部
            return R.ok(activitiesService.getPageAll(
                    pageIndex, pageSize, teamName, activeName));
        } else {
//            return R.ok(activitiesService.getPageByUserId(
//                    pageIndex, pageSize, user.getId(), teamName, activeName));
            return R.ok(activitiesService.getPageAll(
                    pageIndex, pageSize, teamName, activeName));

        }
    }

    /**
     * 添加活动
     *
     * @param activities 活动信息
     * @return 结果
     */
    @PostMapping("/add")
    public R addInfo(Activities activities) {
        activities.setId(UUIDUtils.makeIDByCurrent());
        activitiesService.add(activities);
        return R.ok();
    }

    /**
     * 更新活动
     *
     * @param activities 活动信息
     * @return 结果
     */
    @PostMapping("/upd")
    public R updInfo(Activities activities) {
        activitiesService.update(activities);
        return R.ok();
    }

    /**
     * 删除活动
     *
     * @param id 活动id
     * @return 结果
     */
    @PostMapping("/del")
    public R delInfo(String id) {
        Activities activities = activitiesService.getOne(id);
        activitiesService.delete(activities);
        return R.ok();
    }
}